package tree.test530;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/18 10:10
 */
public class Solution1 {

    // 中序遍历，找差值
    List<Integer> res = new ArrayList<>();
    int ans = Integer.MAX_VALUE;

    public int getMinimumDifference(TreeNode root) {
        inorder(root);
        int len = res.size();
        return ans;
    }

    private void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        if (res.size() > 0) {
            ans = Math.min(ans, Math.abs(root.val - res.get(res.size() - 1)));
        }
        res.add(root.val);
        inorder(root.right);
    }
}
